package com.ggh.jinjilive.ui.mine.address;

import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.mall.goods.thegoods.TheGoodsAddressMobile;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 收货地址
 */
public class TheGoodsViewModel extends BaseViewModel{
    //获取收货地址
    public MutableLiveData<List<TheGoodsAddressMobile>> TheGoodsAddressMobileList = new MutableLiveData<>();

    //地址是否添加成功
    public MutableLiveData<Boolean> address = new MutableLiveData<>();
    public TheGoodsViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);

    }

    /**
     * 获取收货地址
     */
    public void receiveAddressList(){
        Map<String, Object> map = new HashMap<>();
        addSubscribe(apiCallInterface.receiveAddressList(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<List<TheGoodsAddressMobile>>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                List<TheGoodsAddressMobile> theGoodsAddressMobileList = (List<TheGoodsAddressMobile>) resultEntity.getData();
                TheGoodsAddressMobileList.setValue(theGoodsAddressMobileList);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                TheGoodsAddressMobileList.setValue(null);
            }
        }));
    }

    /**
     * 地址添加成功
     * @param map
     */
    public void addReceiveAddress(Map<String, Object> map){
        addSubscribe(apiCallInterface.addReceiveAddress(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                ToastUtils.make().show(resultEntity.getMsg());
                address.setValue(true);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
            }
        }));
    }


    /**
     * 修改地址
     * @param map
     */
    public void editReceiveAddress(Map<String, Object> map){
        addSubscribe(apiCallInterface.editReceiveAddress(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                ToastUtils.make().show(resultEntity.getMsg());
                address.setValue(true);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
            }
        }));
    }


    /**
     * 删除地址
     * @param ids
     */
    public void delReceiveAddress(int ids){
        Map<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        addSubscribe(apiCallInterface.delReceiveAddress(map).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Object>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                ToastUtils.make().show(resultEntity.getMsg());
                //获取收货地址
                receiveAddressList();
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
            }
        }));
    }


}
